package group2assignone.ucm.customer;

import group2assignone.dao.MyDAO;
import group2assignone.func.Authentication;
import group2assignone.model.Customer;
import group2assignone.model.Flight;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.UITool;
import group2assignone.view.customer.AvailableFlightsView;
import group2assignone.view.customer.SearchFlightsView;
import java.util.ArrayList;

/**
 * Use Case to search for available flights based on a
 * from location, destination and date
 * @author group2
 */
public class UCSearchFlights implements UCController {

    private Customer customer;
    private boolean ok,  authOk;
    private Authentication auth;
    private SearchFlightsView view;
    private Flight flight;
    private ArrayList<Flight> availFlights;

    /**
     * The customer field is used for personalisation of view.
     * @param view
     * @param customer
     */
    public UCSearchFlights(Customer customer, SearchFlightsView view) {
        this.view = view;
        this.customer = customer;
    }

    /**
     * Use Case: Search Flights
     * Pre-Condition: Search Flight UI
     * Post-Condition: UI is updated
     * 1. User inputs flight details
     * 2. User selects 'Search Flight'
     * 3. System loads flight details
     * 4. System searches and loads flights with those details
     * 5. System updates the UI to show the available flights
     * 4a. - user enters invalid/non existing flight details
     *     - system notifies user and prompts for new entry
     */
    public void run() {
        auth = new Authentication();
        flight = new Flight();
        availFlights = new ArrayList();//will be calculated later

        view.upLoad(flight);
        authOk = auth.checkFlightFields(flight);
        if (authOk) {
            //find files from database, return array of flight numbers, availabilities, price
            MyDAO coms = UCStartup.getInstance().getDAO();
            availFlights = coms.sQLSearchFlights(flight);
            AvailableFlightsView gui = new AvailableFlightsView(customer);
            gui.go();
            gui.displayFlights(availFlights);
            view.dispose();
        } else {
            UITool.displayError(auth.getErrorMsg(), view);
        }
    }

    /**
     * Function to create dummy flights for testing
     */
    public void dummyFlights() {
        Flight f1 = new Flight();
        Flight f2 = new Flight();
        Flight f3 = new Flight();

        f1.setFlightNum("1");
        f1.setDepTime("9am");
        f1.setCost("$20");
        f1.setSeatCap(30);

        f2.setFlightNum("2");
        f2.setDepTime("11.30am");
        f2.setCost("$10");
        f2.setSeatCap(50);

        f3.setFlightNum("3");
        f3.setDepTime("4pm");
        f3.setCost("$40");
        f3.setSeatCap(5);

        availFlights.add(f1);
        availFlights.add(f2);
        availFlights.add(f3);
    }
}
